﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcctrueWMS.Foundation.Data
{
    /// <summary>
    /// 实现按key 和 index 都可以索引  刘标
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public class Sorte<Tkey, TValue>
    {

        protected System.Collections.Generic.Dictionary<Tkey, TValue> mValue;
        protected List<Tkey> mKey;
        //private Hashtable mValue;

        /// <summary>
        /// 构造函数
        /// </summary>
        public Sorte()
        {
            mKey = new List<Tkey>();
            mValue = new System.Collections.Generic.Dictionary<Tkey, TValue>();
        }

        /// <summary>
        /// 添加到字典
        /// </summary>
        /// <param name="key">唯一的主健</param>
        /// <param name="value">值</param>
        public virtual void Add(Tkey key, TValue value)
        {
            if (mKey.Contains(key))
            {
                return;//由于暂时没找到出现重复数据的源头，为了保证业务能够走下去，所以做了此处理，但是数据库中的异常数据会出现累积
                throw new Exception("重复的Key值!");
            }
            this.mKey.Add(key);
            this.mValue.Add(key, value);
        }

        /// <summary>
        /// 清除字典
        /// </summary>
        public virtual void Clear()
        {
            mValue.Clear();
            mKey.Clear();
        }

        /// <summary>
        /// 索引
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual TValue this[Tkey key]
        {

            get
            {
                try
                {
                    return mValue[key];
                }
                catch
                {
                    return default(TValue);
                }
            }
            set
            {
                mValue[key] = value;
            }
        }

        /// <summary>
        /// 索引 int
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public virtual TValue this[int index]
        {
            get
            {
                if ((index < 0) || index >= mValue.Count)
                {
                    throw new Exception("超出索引范围!");
                }
                Tkey obj = mKey[index];
                return mValue[obj];
            }
            set
            {
                if ((index < 0) || index >= mValue.Count)
                {
                    throw new Exception("超出索引范围!");
                }
                Tkey obj = mKey[index];
                mValue[obj] = value;
            }
        }

        public virtual void Remove(Tkey key)
        {
            if (mKey.Contains(key) && mValue.ContainsKey(key))
            {
                mValue.Remove(key);
                mKey.Remove(key);
            }
        }

        public virtual void Remove(int index)
        {
            if ((index < 0) || index >= mValue.Count)
            {
                throw new Exception("超出索引范围!");
            }
            Tkey obj = mKey[index];
            mValue.Remove(obj);
            mKey.Remove(obj);
        }

        public virtual int Count
        {
            get
            {
                return mValue.Count;
            }
        }

        public bool ContainsKey(Tkey key)
        {
            return mValue.ContainsKey(key);
        }
    }
}
